package com.zhanghu.backendZray.controller.accounting;

import com.zhanghu.backendZray.annotation.MenuNeed;
import com.zhanghu.backendZray.pojo.dto.ApiResponse;
import com.zhanghu.backendZray.pojo.request.OnlyIdRequest;
import com.zhanghu.backendZray.pojo.request.accounting.AccountingQueryRequest;
import com.zhanghu.backendZray.pojo.request.accounting.AccountingRequest;
import com.zhanghu.backendZray.pojo.vo.accounting.AccountingSummaryVo;
import com.zhanghu.backendZray.pojo.vo.accounting.AccountingVo;
import com.zhanghu.backendZray.service.AccountingService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 记账 前端控制器
 * </p>
 *
 * @author zhangdong
 * @since 2024-12-19
 */
@RestController
@RequestMapping("/accounting")
@RequiredArgsConstructor
public class AccountingController {
    private final AccountingService accountingService;

    /**
     * 新增/修改
     */
    @MenuNeed(menuName = "accounting")
    @PostMapping("save")
    public ApiResponse save(@Valid @RequestBody AccountingRequest request) {
        accountingService.saveAccounting(request);
        return ApiResponse.ok();
    }

    /**
     * 按年月查询
     */
    @MenuNeed(menuName = "accounting")
    @PostMapping("listByYearMonth")
    public ApiResponse<List<AccountingVo>> listByYearMonth(@Validated(AccountingQueryRequest.NeedYearMonth.class) @RequestBody AccountingQueryRequest queryRequest) {
        List<AccountingVo> list = accountingService.listByYearMonth(queryRequest);
        return ApiResponse.ok(list);
    }

    /**
     * 删除
     */
    @MenuNeed(menuName = "accounting")
    @PostMapping("delete")
    public ApiResponse delete(@Valid @RequestBody OnlyIdRequest onlyIdRequest) {
        accountingService.delete(onlyIdRequest.getId().intValue());
        return ApiResponse.ok();
    }

    /**
     * 年度汇总
     *
     * @param request
     * @return
     */
    @MenuNeed(menuName = "accounting_summary")
    @PostMapping("yearSummary")
    public ApiResponse<AccountingSummaryVo> yearSummary(@Validated(AccountingQueryRequest.NeedYear.class) @RequestBody AccountingQueryRequest request) {
        AccountingSummaryVo vo = accountingService.yearSummary(request);
        return ApiResponse.ok(vo);
    }

    /**
     * 月度汇总
     *
     * @param request
     * @return
     */
    @MenuNeed(menuName = "accounting_summary")
    @PostMapping("monthSummary")
    public ApiResponse<AccountingSummaryVo> monthSummary(@Validated(AccountingQueryRequest.NeedYearMonth.class) @RequestBody AccountingQueryRequest request) {
        AccountingSummaryVo vo = accountingService.monthSummary(request);
        return ApiResponse.ok(vo);
    }

}
